package com.eptok.gw.szcups.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.eptok.gw.szcups.dao.bo.Cupsgwlog;
import com.eptok.gw.szcups.dao.bo.Cupsreveral;
import com.sun.org.apache.commons.collections.MapUtils;

public class JdbcDao {

	private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}



	private JdbcTemplate getJdbcTemplate() {
		return new JdbcTemplate(this.dataSource);
	}
	
	public long querySequences(String seqName){
		String sql = "select "+seqName +".nextval from dual";
		return getJdbcTemplate().queryForLong(sql);
	}
	
	public void insertCupsLog(Cupsgwlog cupsgwlog){
		String sql = "insert into CUPSGWLOG(id,MERCHANTID,TERMINALID,MESSAGEID,MESSAGECODE,SYTEMTRACE,ENTRYMODE,CONDITIONCODE,RETRIEVALNUMBER,CURRENCYCODE," +
				"PROCESSCODE,ACQUIREINSTITUTION,FORWARDINSTITUTION,ACCEPTTERMIANL,ACCEPTCODE,ACCEPTNAME,CUSTOMIZINGMESSAGE,BANKACCOUNT,AMOUNT,GATEWAYTYPE," +
				"REQUETOR,STATUS,REQTRACEID,REQTIME,HEXDATA) " +
				"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate,?)";
		Object[] args = {cupsgwlog.getId(),
				         cupsgwlog.getMerchantId(),
				         cupsgwlog.getTerminalId(),
				         cupsgwlog.getMessageId(),
				         cupsgwlog.getMessageCode(),
				         cupsgwlog.getSytemtrace(),
		                 cupsgwlog.getEntryMode(),
		                 cupsgwlog.getConditionCode(),
		                 cupsgwlog.getRetrievalNumber(),
		                 cupsgwlog.getCurrencyCode(),
		                 cupsgwlog.getProcessCode(),
		                 cupsgwlog.getAcquireInstitution(),
		                 cupsgwlog.getForwardInstitution(),
		                 cupsgwlog.getAcceptTermianl(),
		                 cupsgwlog.getAcceptCode(),
		                 cupsgwlog.getAcceptName(),
		                 cupsgwlog.getCustomizingMessage(),
		                 cupsgwlog.getBankAccount(),
		                 cupsgwlog.getAmount(),
		                 cupsgwlog.getGateWayType(),
		                 cupsgwlog.getRequetor(),
		                 cupsgwlog.getStatus(),
		                 cupsgwlog.getReqtraceId(),
		                 cupsgwlog.getHexData()
						};
		getJdbcTemplate().update(sql,args);
	}
	
	public void insertCupsreveral(Cupsreveral cupsreveral){
		String sql = " insert into CUPSREVERAL(ID,REQTRACEID,CREATETIME,RETRYCOUNT,STATUS,GATEWAYTYPE) " +
				"values(CUPSREVERAL_SEQ.nextval,?,sysdate,'0','0','01')";
		Object[] args = {cupsreveral.getReqtraceId()};
		getJdbcTemplate().update(sql,args);
		
	}
	
	public void updateCupsLog(Cupsgwlog cupsgwlog){
		String sql = "update CUPSGWLOG set SETTLEMENT=?,RESPONSECODE=?,DESCRIPTION=?,STATUS=?,RSPTIME=sysdate,RSPTRACEID=?,REMARK=? where id=?";
		Object[] args = {cupsgwlog.getSettlement(),
				         cupsgwlog.getResponseCode(),
				         cupsgwlog.getDescription(),
				         cupsgwlog.getStatus(),
				         cupsgwlog.getRsptraceId(),
		                 cupsgwlog.getRemark(),
		                 cupsgwlog.getId(),
				        };
		getJdbcTemplate().update(sql,args);
	}
	
	
	public void updateCupsreveral(Cupsreveral cupsreveral){
		String sql = "update CUPSREVERAL set CREATETIME=sysdate,RETRYCOUNT=?,STATUS=? where id=?";
		Object[] args = {cupsreveral.getRetryCount(),
				         cupsreveral.getStatus(),
				         cupsreveral.getId()
				        };
		getJdbcTemplate().update(sql,args);
	}
	
	
	/**
	 * 根据商户号/原流水号查询需要冲正的hexData
	 * @return
	 */
	public String queryHexDataByOldRequireId(String merchantId,String oldReqSerialNumber){
		String sql = "select * from CUPSGWLOG where MERCHANTID=? and REQTRACEID=? and GATEWAYTYPE='01'";
		Object[] args = {merchantId,oldReqSerialNumber};
		List<Map<String,Object>> list = getJdbcTemplate().queryForList(sql,args);
		if(list.size()>0){
			Map<String,Object> map = list.get(0);
			return MapUtils.getString(map, "HEXDATA");
		}else{
			return null;
		}
		
	}
	
	/**
	 * 查询待冲正记录
	 * @return
	 */
	public List<Cupsreveral> queryRerveral(){
		String sql = "select * from CUPSREVERAL where retryCount <=3 and (status='0' or status='3') and rownum<=5 and GATEWAYTYPE='01' order by createTime,status";
		List<Map<String,Object>> listmap = getJdbcTemplate().queryForList(sql);
		List<Cupsreveral> list = new ArrayList<Cupsreveral>();
		for (Object object : listmap) {
			Map<String, Object> map = (Map<String, Object>) object;
			Cupsreveral cupsreveral = new Cupsreveral();
			cupsreveral.setId(MapUtils.getLong(map, "ID"));
			cupsreveral.setRetryCount(MapUtils.getInteger(map, "RETRYCOUNT"));
			cupsreveral.setReqtraceId(MapUtils.getString(map, "REQTRACEID"));
			list.add(cupsreveral);
		}
		return list;
	}
}
